package com.yanqu.road.server.cmd.activity;

import com.yanqu.road.entity.enums.eCardType;
import com.yanqu.road.entity.player.UserCard;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.CardProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CardModule;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;


@Cmd(code = Protocol.S_CARD_GET_REWARD, desc = "特权卡领取奖励")
public class CardGetRewardCmd extends AbstractCommand {

	private int type;

	private Property reward;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		CardProto.PrivilegeCardRewardReqMsg reqMsg = CardProto.PrivilegeCardRewardReqMsg.parseFrom(packet.getBytes());
		type = reqMsg.getType();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		player.getModule(CardModule.class).autoSendReward();
		if(!player.getModule(CardModule.class).isInCardTime(type)){
			ret = GameErrorCode.E_CARD_OVER_TIME;
			return;
		}
		if(player.getModule(CardModule.class).hasGetReward(type)){
			ret = GameErrorCode.E_CARD_HAS_GET_REWARD;
			return;
		}
		reward = player.getModule(CardModule.class).getReward(type);
	}

	@Override
	public void sendData(GamePlayer player) {
		CardProto.PrivilegeCardRewardRespMsg.Builder respMsg = CardProto.PrivilegeCardRewardRespMsg.newBuilder();
		respMsg.setRet(ret);
		if(0 == ret){
			UserCard userCard = player.getModule(CardModule.class).getUserCard();
			respMsg.setType(type);
			if(eCardType.MonthCard.getValue() == type) {
				respMsg.setGetRewardTime(userCard.getGetMonthCardRewardTime());
			}else if(eCardType.YearCard.getValue() == type){
				respMsg.setGetRewardTime(userCard.getGetYearCardRewardTime());
			}
			respMsg.setReward(PropertyHelper.parsePropertyToString(reward));
		}
		player.sendPacket(Protocol.U_CARD_GET_REWARD, respMsg);
	}

}
